package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult empLogin(String name, String password, HttpSession session) {
        JsonResult jsonResult = new JsonResult();

        Employee employee = employeeService.selectByName(name, password);
        UserContext.setCurrentUser(employee);
        List<Permission> permissionList = employeeService.selectAllPermission(employee.getId());
        UserContext.setCurrentPermissions(permissionList);

        return jsonResult;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/static/login.html";
    }

    @RequestMapping("/nopermission")
    public String noPermission() {
        return "common/nopermission";
    }

    @RequestMapping("/error")
    public String error() {
        return "common/error";
    }
}
